package com.totalinlink.dsl.poi.wrappers

import com.totalinlink.dsl.poi.WorkBookBuilder

/**
 * Created by wangguangyu on 15/5/24.
 */
class SheetWrapper extends NodeWrapper {

    static CLS_MAPS = [:]

    static {
        CLS_MAPS[MultiRowWrapper.class] = { sheet, lst ->
            def rs = sheet.rows
            lst.rows.each {
                it.index = rs.size()
                rs << it
            }
        }
        CLS_MAPS[RowWrapper.class] = { sheet, i ->
            def rs = sheet.rows
            rs << i
            i.index = rs.size()
        }
        CLS_MAPS[MergedRegionWrapper.class] = { sheet, r ->
            def rs = sheet.regions
            rs << r
        }
        CLS_MAPS[ForStyleWrapper.class] = { sheet, style ->
            sheet.forStyles << style
        }
    }

    // the index in workbook
    def index
    // workbook wrapper
    def workbook
    // sheet name
    def name
    // rows wrapper
    def rows = []
    // merged region
    def regions = []
    // for styles
    def forStyles = []
    // pageSetup
    def pageSetup = [:]
    // print area
    def printArea =[:]

    def SheetWrapper(WorkBookBuilder workBookBuilder, Map attributes) {

        super(workBookBuilder, attributes)

        this.workbook = workBookBuilder.workbook
        // check valid workbook
        assert this.workbook
        this.name = attributes?.name
        // page setup
        this.pageSetup = attributes?.pageSetup
        // print area
        this.printArea = attributes?.printArea
        // set current sheet
        workBookBuilder.curSheet = this
    }

    @Override
    def void setNode(child) {
        CLS_MAPS[child.class](this, child)
    }

    @Override
    void syncNode() {

        assert this.workbook.poiObject
        // init sheet
        if (name) {
            this.poiObject = this.workbook.poiObject.getSheet(name)
            if (!this.poiObject) {
                this.poiObject = this.workbook.poiObject.createSheet(name)
            }
        } else {
            this.poiObject = this.workbook.poiObject.createSheet()
        }

        this.workbook.poiObject.setActiveSheet(this.index)

        // sync rows
        rows.each {
            it.syncNode()
        }

        // sync regions
        regions.each {
            it.syncNode()
        }

        // sync styles
        forStyles.each {
            it.syncNode()
        }

        // sync page setup
        if (pageSetup) {
            def ps = poiObject.pageSetup
            ps.paperSize      = pageSetup.paperSize
            ps.scale          = pageSetup.scale
            ps.pageStart      = pageSetup.pageStart
            ps.fitWidth       = pageSetup.fitWidth
            ps.fitHeight      = pageSetup.fitHeight
            // ps.leftToRight = pageSetup.leftToRight
            ps.landscape      = pageSetup.landscape
            ps.validSettings  = pageSetup.validSettings
            ps.draft          = pageSetup.draft
            ps.notes          = pageSetup.notes
            ps.noOrientation  = pageSetup.noOrientation
            ps.usePage        = pageSetup.usePage
            ps.hResolution    = pageSetup.hResolution
            ps.vResolution    = pageSetup.vResolution
            ps.headerMargin   = pageSetup.headerMargin
            ps.footerMargin   = pageSetup.footerMargin
            ps.copies         = pageSetup.copies
            poiObject.setPageSetup(ps)
        }

        // sync print area
        if (printArea) {
            workbook.poiObject.setPrintArea(index, printArea.startColumn, printArea.endColumn,
                    printArea.startRow, printArea.endRow)
        }

    }

    /**
     * get the row in the sheet by index i
     * @param i index
     * @return RowWrapper
     */
    def RowWrapper getRow(i) {
        return (RowWrapper) rows[i]
    }

}
